package com.monkeys.service.impl;

import com.monkeys.Utils.RandomUtil;
import com.monkeys.mapper.TrucksTypeMapper;
import com.monkeys.mapper.VehicleMapper;
import com.monkeys.pojo.TrucksType;
import com.monkeys.pojo.Vehicle;
import com.monkeys.service.VehicleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * @author KuangTeam
 */
@Service
@RequiredArgsConstructor
public class VehicleServiceImpl implements VehicleService {

    private final VehicleMapper vehicleMapper;
    private final TrucksTypeMapper trucksTypeMapper;

    //创建随机参数的一辆待运的货车
    @Override
    public Vehicle createVehicle() {
        Vehicle vehicle = new Vehicle();

        //货车种类和基本信息属性
        Random random = new Random();
        TrucksType trucksType = trucksTypeMapper.getByRandomId(random.nextInt(11) + 1);
        //拷贝
        BeanUtils.copyProperties(trucksType, vehicle);

        //货车品牌、车牌号属性
        vehicle.setBandName(trucksTypeMapper.getBandByRandomId(random.nextInt(16) + 1));
        vehicle.setTypeCode(RandomUtil.generateLicensePlate());

        //货车行为属性:   1:运输中  2:无货休息中  3:运输休息中   4.意外中
        vehicle.setActionId(2);

        //默认车开始都在保养休息,因此速度为0
        vehicle.setSpeed(0.0);

        return vehicle;
    }


    //创建任意辆待运的货车
    @Override
    public List<Vehicle> createVehicles(Long id) {
        List<Vehicle> list = new ArrayList<>();
        for(int i = 1; i <= id; i++){
            Vehicle vehicle = createVehicle();
            list.add(vehicle);
        }
        //由于是同一个位置出发，因此设置统一的经纬度
        Double randLat = RandomUtil.generateRandomNumber6(30, 31);
        Double randLng = RandomUtil.generateRandomNumber6(103,104);

        list.forEach(vehicle -> {
            vehicle.setLat(randLat);
            vehicle.setLng(randLng);
            //插入车表
            vehicleMapper.insert(vehicle);
        });

        return list;
    }


    //更新车辆数据
    @Override
    public void update(Vehicle vehicle) {
        vehicleMapper.update(vehicle);
    }


    //获取所有车辆信息
    @Override
    public List<Vehicle> getAllVehicles() {
        return vehicleMapper.selectAll();
    }


    //删除所有信息
    @Override
    public void deleteAll() {
        vehicleMapper.deleteAll();
    }
}
